package com.zmc.lostfound.auth.controller;

import com.zmc.lostfound.auth.entity.RoleEntity;
import com.zmc.lostfound.auth.service.RoleService;
import com.zmc.lostfound.auth.vo.RoleSaveVO;
import com.zmc.lostfound.common.utils.PageParams;
import com.zmc.lostfound.common.utils.PageResult;
import com.zmc.lostfound.common.utils.Result;
import com.zmc.lostfound.common.valid.AddGroup;
import com.zmc.lostfound.common.valid.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZMC
 * @description 角色信息管理接口
 * @email 2869581855@qq.com
 * @date 2023-07-05 15:09:50
 */
@RestController
@RequestMapping("/role")
@Api(value = "角色信息管理接口", tags = "角色信息管理接口")
public class RoleController {

    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @ApiOperation("分页模糊查询角色信息")
    @PostMapping("/query/page")
    public Result queryPage(@RequestBody @Validated PageParams pageParams) {
        PageResult pageResult = roleService.queryPage(pageParams);
        return Result.ok("查询成功").put("pageResult", pageResult);
    }

    @ApiOperation("按主键id查询角色信息")
    @GetMapping("/query/infoById/{id}")
    public Result queryInfoById(@PathVariable("id") Long id) {
        RoleEntity roleInfo = roleService.queryInfoById(id);
        return Result.ok("查询成功").put("roleInfo", roleInfo);
    }

    @ApiOperation("按用户id查询角色信息")
    @GetMapping("/query/infoByUserId/{userId}")
    public Result queryInfoByUserId(@PathVariable("userId") Long userId) {
        List<RoleEntity> roleInfoList = roleService.queryInfoByUserId(userId);
        return Result.ok("查询成功").put("roleInfoList", roleInfoList);
    }

    @ApiOperation("按权限id查询角色信息")
    @GetMapping("/query/infoByPermissionId/{PermissionId}")
    public Result queryInfoByPermissionId(@PathVariable("PermissionId") Long permissionId) {
        List<RoleEntity> roleInfoList = roleService.queryInfoByPermissionId(permissionId);
        return Result.ok("查询成功").put("roleInfoList", roleInfoList);
    }

    @ApiOperation("新增角色信息")
    @PostMapping("/insertRole")
    public Result insertRole(@RequestBody @Validated(value = {AddGroup.class}) RoleSaveVO roleSaveVO) {
        return roleService.insertRole(roleSaveVO);
    }

    @ApiOperation("修改角色信息")
    @PutMapping("/updateRole")
    public Result updateRole(@RequestBody @Validated(value = {UpdateGroup.class}) RoleSaveVO roleSaveVO) {
        return roleService.updateRole(roleSaveVO);
    }

    @ApiOperation("按角色id删除角色信息")
    @DeleteMapping("/delete/{roleId}")
    public Result deleteByRoleId(@PathVariable("roleId") Long roleId) {
        return roleService.deleteByRoleId(roleId);
    }

}
